cap program drop bycomp

program define bycomp

syntax varlist, ctrls(string) condition(string) outcome(string) xmin(string) xmax(string) xdelta(string) wave(string)

	foreach depvar in `varlist' {

	local depvar `depvar'
	local ctrls `ctrls'
	local condition `condition'
	local outcome `outcome'
	local xmin `xmin'
	local xmax `xmax'
	local xdelta `xdelta'
	local wave `wave'

		reg `depvar' i.treat_control_comp_study2 `ctrls' if `condition'
			margins, dydx(i.treat_control_comp_study2)
				matrix `depvar' = r(table)
					matselrc `depvar' `depvar'_hom, r(1, 5, 6) c(2)
					matselrc `depvar' `depvar'_mix, r(1, 5, 6) c(3)
	
				graph set window fontface "Times"
				coefplot (matrix(`depvar'_hom[1,]), ci((`depvar'_hom[2,] `depvar'_hom[3,])) recast(scatter) ciopts(lcolor(black) lpattern(solid)) mcolor(black) msymbol(square)) ///
					(matrix(`depvar'_mix[1,]), ci((`depvar'_mix[2,] `depvar'_mix[3,])) recast(scatter) ciopts(lcolor(black) lpattern(longdash)) mcolor(black) msymbol(triangle)), ///
						name(`depvar'_bycomp, replace) scheme(plotplain) ///
						xtitle("Treatment effect on `outcome'", margin(medsmall)) xlabel(`xmin'(`xdelta')`xmax', nogrid) ///
						ytitle("", margin(medsmall)) ylabel(, nogrid notick nolabels) ///
						legend(on nostack position(6) cols(3)order(2 "Homogeneous" 4 "Mixed")) ///
						xline(0, lcolor(black) lpattern(dash)) 

							graph export "Output/`depvar'_bycomp_`wave'.pdf", as(pdf) replace
							cap graph export "$dir_Overleaf/Figures/`depvar'_bycomp_`wave'.pdf", as(pdf) replace
							cap graph export "$dir_Overleaf_JG/Figures/`depvar'_bycomp_`wave'.pdf", as(pdf) replace
	
	}
	
end
		
		
